/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.controllers;

import bl.controllers.JAXBController;
import bl.controllers.PeriodsJpaController;
import bl.controllers.UserJpaController;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import entities.CvType;
import entities.Document;
import entities.JobType;
import entities.LanguageRecordType;
import entities.MscType;
import entities.PersonalInfoType;
import entities.ProjectType;
import entities.TitleType;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class FormController {

    private JAXBController jaxbc;
    private Integer userId;
    private UserJpaController userJpaController;
    private PeriodsJpaController periodsJpaController;

    public FormController(Integer userId) {
        jaxbc = new JAXBController();
        this.userId = userId;
    }

    public boolean isOutOfDate() {
        //TODO check if current date is in period
        return false;
    }

    public boolean isCommited() {
        userJpaController = new UserJpaController();
        if (userJpaController.findUser(userId).getDocumentCollection().isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public BeanItem getPersonalInfoDataSource() {
        PersonalInfoType personalInfoType = new PersonalInfoType();
        BeanItem beanItem = new BeanItem(personalInfoType);
        return beanItem;
    }

    public BeanItemContainer getStudiesBeanItemContainer() {
        return new BeanItemContainer(TitleType.class);
    }

    public BeanItemContainer getProjectsBeanItemContainer() {
        return new BeanItemContainer(ProjectType.class);
    }

    public BeanItemContainer getLanguagesBeanItemContainer() {
        return new BeanItemContainer(LanguageRecordType.class);
    }

    public BeanItemContainer getJobsBeanItemContainer() {
        return new BeanItemContainer(JobType.class);
    }

    public BeanItemContainer getCvsBeanItemContainer() {
        return new BeanItemContainer(CvType.class);
    }

    public BeanItemContainer getMscBeanItemContainer() {
        return new BeanItemContainer(MscType.class);
    }

    public void setAreasOfStudy(String area1Value, String area2Value, String area3Value) {
        jaxbc.setAreasOfStudy(area1Value, area2Value, area3Value);
    }

    public void setPersonalInfo(Item personalInfoTypeItem) {
        PersonalInfoType personalInfoType = (PersonalInfoType) ((BeanItem) personalInfoTypeItem).getBean();
        jaxbc.setPersonalInfo(personalInfoType);
    }

    public void setStudies(Collection<TitleType> titleTypes) {
        jaxbc.setStudies(titleTypes);
    }

    public void setProjects(Collection<ProjectType> projectTypes) {
        jaxbc.setProjects(projectTypes);
    }

    public void setAwards(Collection<String> awards) {
        jaxbc.setAwards(awards);
    }

    public void setLanguages(Collection<LanguageRecordType> languageRecordTypes) {
        jaxbc.setLanguages(languageRecordTypes);
    }

    public void setJobs(Collection<JobType> jobTypes) {
        jaxbc.setJobs(jobTypes);
    }

    public void setResearch(Collection<String> research) {
        jaxbc.setResearch(research);
    }

    public void setCvs(Collection<CvType> cvTypes) {
        jaxbc.setCvs(cvTypes);
    }

    public void setOtherMsc(Collection<MscType> mscTypes) {
        jaxbc.setOtherMsc(mscTypes);
    }

    public void setOtherInfo(String value) {
        jaxbc.setOtherInfo(value);
    }

    public void save() {
        Document document = new Document();
        userJpaController = new UserJpaController();
        document.setUser(userJpaController.findUser(userId));
        jaxbc.save(document);
    }
}
